#ifndef __SERVER__
#define __SERVER__

#include <windows.h>
#include <vector>

#include "../Common/CommonThread.h"

#include "ServerBase.h"
#include "ServerBroadcaster.h"
#include "ServerClientManager.h"
#include "ServerObjectManager.h"
#include "ServerServicer.h"



const unsigned int MAX_CLIENT = 8;

enum ERROR_CODE
{	
	INVALID_NETWORK
};

class server : public server_base {
private:
	bool					m_bLoopBreaker;
	const unsigned int		m_MaxClient;
	
	thread_manager	m_ThreadManager;
	broadcaster		m_Broadcaster;
	servicer		m_Servicer;
	client_manager	m_ClientManager;
	object_manager	m_ObjectManager;
	network*		m_UdpNetwork;
	
	vector<int>			m_CloseThreadIdList;	
	CRITICAL_SECTION	m_CriticalSection;	
	CRITICAL_SECTION	m_TextCriticalSection;
	HANDLE				m_DeleteEvent;

	bool DeleteClient(const int ClientCode, const int ThreadNumber, const ip IP, const int UDPPort);

public:
	server(unsigned int MaxClient = MAX_CLIENT);
	~server(void);

	bool Begin(int ServicePort, int BroadcastPort, int RelayPort);	
	bool End(void);
	bool Loop(void);
	
	bool NewClient(network* NewServiceNetwork, network* NewBroadcastNetwork);

	void DisplayText(const wchar_t*	Text, ...);	

	bool DeleteClient(unsigned int ClientCode);
	bool DeleteClient(network* ServiceOrBroadcast);
	void CriticalError(int ErrorCode);
	void NormalError(int ErrorCode);
};


#endif